package echat.future.cn.echat.home

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.zhusx.core.app._BaseAppCompatActivity
import com.zhusx.core.imp.SimpleRecyclerAdapter
import com.zhusx.core.interfaces.IHttpResult
import com.zhusx.core.network.HttpRequest
import echat.future.cn.echat.R
import echat.future.cn.echat.home.bean.DearEntity
import echat.future.cn.echat.home.network.LoadData
import echat.future.cn.echat.home.network.SimpleHttpListener
import echat.future.cn.echat.home.tools.loadHeaderImage
import kotlinx.android.synthetic.main.activity_dear_ranking.*

/**
 *
 * Author  zhusx
 * Email   327270607@qq.com
 * Create  2018/8/9 23:18
 */
class DearRankingActivity : _BaseAppCompatActivity() {
    private val listData by lazy {
        LoadData<List<DearEntity>>(LoadData.Api.用户亲密榜, this)
                .apply {
                    _setOnLoadingListener(object : SimpleHttpListener<List<DearEntity>>() {
                        override fun onLoadComplete(api: LoadData.Api, request: HttpRequest<Any>, result: IHttpResult<List<DearEntity>>) {
                            initView(result.data)
                        }
                    })
                }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_dear_ranking)
        val id = intent.getStringExtra(_EXTRA_String_ID)
        listData._loadData(id)
        iv_return.setOnClickListener { finish() }
    }

    private fun initView(data: List<DearEntity>) {
        recyclerView.adapter = object : SimpleRecyclerAdapter<DearEntity>(R.layout.list_item_dear, data) {
            override fun bindViewHolder(holder: _ViewHolder, i: Int, s: DearEntity) {
                holder.run {
                    getView<ImageView>(R.id.iv_image).loadHeaderImage(s.user_avatar)
                    setText(R.id.tv_name, s.username)
                    setText(R.id.tv_level, "LV.${s.level ?: "0"}")
                    setText(R.id.tv_follow, s.close_num)
                    when (i) {
                        0 -> {
                            getView<ImageView>(R.id.iv_index).visibility = View.VISIBLE
                            getView<ImageView>(R.id.iv_index).setImageResource(R.drawable.xinmidu_diyi)
                            getView<TextView>(R.id.tv_index).text = ""
                        }
                        1 -> {
                            getView<ImageView>(R.id.iv_index).visibility = View.VISIBLE
                            getView<ImageView>(R.id.iv_index).setImageResource(R.drawable.qinmidu_dier)
                            getView<TextView>(R.id.tv_index).text = ""
                        }
                        2 -> {
                            getView<ImageView>(R.id.iv_index).visibility = View.VISIBLE
                            getView<ImageView>(R.id.iv_index).setImageResource(R.drawable.qinmidu_disan)
                            getView<TextView>(R.id.tv_index).text = ""
                        }
                        else -> {
                            getView<ImageView>(R.id.iv_index).visibility = View.GONE
                            getView<ImageView>(R.id.iv_index).setImageResource(R.drawable.qinmidu_disan)
                            getView<TextView>(R.id.tv_index).text = "NO.${i + 1}"
                        }
                    }
                    rootView.setOnClickListener { BottomDearDialog(it.context, s).show() }
                }
            }
        }
    }
}